package cn.ccit.DAOImp;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.ccit.DAO.SystemRoleDAO;
import cn.ccit.entites.SystemRole;
import cn.ccit.entites.Terminal;
import cn.ccit.util.MyDBUtil;

public class SystemRoleDAOimp implements SystemRoleDAO {
	private Session session;
	private static final Log log = LogFactory.getLog(TerminalDAOimp.class);
	@Override
	public boolean addSystemRole(SystemRole ua) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.save(ua);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("add ua fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public boolean removeSystemRole(SystemRole ua) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.delete(ua);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("delete Terminal fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public boolean changeSystemRoleInfo(SystemRole ua) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.update(ua);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("update SystemRole fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public List<SystemRole> getSomeSystemRoles(int start, int limit) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from SystemRole as sr order by id desc";
				Query query=session.createQuery(hql);
				Long count=(Long)session.createQuery("select count(*) from SystemRole").uniqueResult();
				if(count>limit){
					query.setFirstResult(start);
					query.setMaxResults(limit);
				}
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("getSomeSystemRole fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public SystemRole searchSystemRoleByID(int id) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from SystemRole as sr where sr.id=?";
				Query query=session.createQuery(hql);
				query.setInteger(0, id);
				SystemRole sr=(SystemRole)query.list().get(0);
				session.beginTransaction().commit();
				return sr;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("searchSystemRoleByID fail",e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

}
